import 'package:flutter/material.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_model/video_model.dart';
import 'package:tiktokadult/hj_page/short_video/view/left_menu/short_left_item.dart';
import 'package:tiktokadult/hj_page/short_video/view/right_menu/short_right_menu.dart';
import 'package:tiktokadult/tools_base/global_store/local_ads_info_store.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:video_player/video_player.dart';

import '../../../hj_model/splash/watch_count_model.dart';
import '../../../hj_utils/global.dart';

typedef MenuSwitchCallback = void Function(bool); // true 显示

class ShortVideoMenu extends StatefulWidget {
  static bool isShow = true;
  static final List<MenuSwitchCallback> _listenList = [];

  static addListen(MenuSwitchCallback callback) {
    _listenList.add(callback);
  }

  static removeLister(MenuSwitchCallback callback) {
    _listenList.remove(callback);
  }

  static notifyListeners() {
    for (var callback in _listenList) {
      callback(isShow);
    }
  }

  final VideoPlayerController? controller;
  final VideoModel? model;
  final WatchCount? watchCount;
  final VoidCallback? onBuy;
  const ShortVideoMenu({
    super.key,
    this.controller,
    this.model,
    this.watchCount,
    this.onBuy,
  });

  @override
  State<StatefulWidget> createState() {
    return _ShortVideoMenuState();
  }
}

class _ShortVideoMenuState extends State<ShortVideoMenu> {
  @override
  void initState() {
    super.initState();
    ShortVideoMenu.addListen(_menuListen);
  }

  @override
  void dispose() {
    ShortVideoMenu.removeLister(_menuListen);
    super.dispose();
  }

  void _menuListen(bool isShow) {
    if (mounted) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.only(bottom: 4),
      child: Stack(
        alignment: Alignment.center,
        children: [
          Container(
            padding: EdgeInsets.only(bottom: getAdvByTypeSync(33).isEmpty ? 40 : 56),
            child: Row(
              children: [
                Expanded(
                  child: Visibility(
                    visible: ShortVideoMenu.isShow,
                    child: ShortLeftItem(
                      widget.model,
                      widget.controller,
                      watchCount: widget.watchCount,
                      onBuy: widget.onBuy,
                    ),
                  ),
                ),
                const SizedBox(width: 8),
                Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    ShortRightMenu(
                      videoModel: widget.model,
                      controller: widget.controller,
                    ),
                  ],
                ),
                12.sizeBoxW,
              ],
            ),
          ),
        ],
      ),
    );
  }
}
